﻿var amCharts = new AmCharts.AmSerialChart;

amCharts.categoryField = "date";
amCharts.autoMargins = !0;
amCharts.marginTop = 0;
amCharts.marginBottom = 0;
amCharts.marginLeft = 0;
amCharts.marginRight = 0;
amCharts.addClassNames = !0;

var a = amCharts.categoryAxis;
a.parseDates = !1;
a.gridAlpha = 0;
a.axisColor = "#DADADA";

var o = new AmCharts.ValueAxis;
o.axisAlpha = 0.03;
o.gridAlpha = 0.1;
o.inside = !0;
o.dashLength = 1;

var guide1 = new AmCharts.Guide;
guide1.value = -1;
guide1.inside = true;
guide1.dashLength = 2;
guide1.color = "#E63234";
guide1.lineAlpha = 0.9;
guide1.lineColor = "#E63234";
guide1.position = "right";
guide1.label = "止盈";
o.addGuide(guide1);

var guide2 = new AmCharts.Guide;
guide2.inside = true;
guide2.value = -1;
guide2.dashLength = 2;
guide2.color = "#19962a";
guide2.lineAlpha = 0.9;
guide2.lineColor = "#19962a";
guide2.position = "right";
guide2.label = "止损";
o.addGuide(guide2);
amCharts.addValueAxis(o);

var d = new AmCharts.ChartCursor;
d.categoryBalloonDateFormat = "JJ:NN";
d.zoomable = !1;
amCharts.addChartCursor(d);

var line_amGraph = new AmCharts.AmGraph;
line_amGraph.type = "smoothedLine";
line_amGraph.valueField = "visits";
line_amGraph.lineAlpha = 1;
line_amGraph.lineColor = "#81bceb";
line_amGraph.fillAlphas = .3;
line_amGraph.classNameField = "bulletClass";
line_amGraph.bullet = "round";
line_amGraph.bulletSizeField = "townSize";
line_amGraph.bulletBorderColor = "#DADADA";
line_amGraph.bulletBorderAlpha = 1;
line_amGraph.bulletBorderThickness = 1;
line_amGraph.bulletColor = "#81bceb";

var kline_amGraph = new AmCharts.AmGraph;
kline_amGraph.type = "candlestick";
kline_amGraph.lineColor = "#e63234";
kline_amGraph.fillColors = "#e63234";
kline_amGraph.negativeLineColor = "#1eb83f";
kline_amGraph.negativeFillColors = "#1eb83f";
kline_amGraph.fillAlphas = 1;
kline_amGraph.openField = "open";
kline_amGraph.highField = "high";
kline_amGraph.lowField = "low";
kline_amGraph.closeField = "close";
kline_amGraph.valueField = "close";
kline_amGraph.balloonText = "<b>[[date]]</b><br/>开盘价:<b>[[open]]</b><br/>最高价:<b>[[high]]</b><br/>最低价:<b>[[low]]</b><br/>收盘价:<b>[[close]]</b><br/>";
kline_amGraph.balloonColor = "#FF8040";

amCharts.write("container");

function InitializaChart(jsonDate, position_order, txt_len, type, isRest) {
    // console.log("1:------"+isRest);
    // console.log(txt_len);
    var new_game_data = [];
    if (type == "l") {
        for (var i=0; i<jsonDate.length; i++) {
            if (jsonDate[i] !== undefined) {
                var d = [];
                d[0] = jsonDate[i].time;
                d[1] = parseFloat(jsonDate[i].close_value).toFixed(txt_len);
                new_game_data.push(d);
            }
        }
    } else {
        for (var i=0; i<jsonDate.length; i++) {
            if (jsonDate[i] !== undefined) {
                var d = [];
                d[0] = jsonDate[i].time;
                d[1] = parseFloat(jsonDate[i].open_value).toFixed(txt_len);
                d[2] = parseFloat(jsonDate[i].max_value).toFixed(txt_len);
                d[3] = parseFloat(jsonDate[i].min_value).toFixed(txt_len);
                d[4] = parseFloat(jsonDate[i].close_value).toFixed(txt_len);
                new_game_data.push(d);
            }
        }
    }

    if (isRest == 1) {
        addGuide(position_order, txt_len);
        if (type == "k") {
            InitializaK(new_game_data);
        } else {
            InitializaM(new_game_data);
        }
    } else {
        var new_game_data = [];
        for (var i=1; i<jsonDate.length; i++) {
            var d = [];
            d[0] = jsonDate[i].time;
            d[1] = jsonDate[0].close_value;
            new_game_data.push(d);
        }
        InitializaM(new_game_data);
    }
}

function upAmChartsDate(vdata, type, txt_len) {
    // console.log("2:------"+type);
    var new_game_data = [];
    if (type == "l") {
        for (var i=0; i<vdata.length; i++) {
            if (vdata[i] !== undefined) {
                var d = [];
                d[0] = vdata[i].time;
                d[1] = parseFloat(vdata[i].close_value).toFixed(txt_len);
                new_game_data.push(d);
            }
        }
    } else {
        for (var i=0; i<vdata.length; i++) {
            if (vdata[i] !== undefined) {
                var d = [];
                d[0] = vdata[i].time;
                d[1] = parseFloat(vdata[i].open_value).toFixed(txt_len);
                d[2] = parseFloat(vdata[i].max_value).toFixed(txt_len);
                d[3] = parseFloat(vdata[i].min_value).toFixed(txt_len);
                d[4] = parseFloat(vdata[i].close_value).toFixed(txt_len);
                new_game_data.push(d);
            }
        }
    }

    var data = [];
    if (type == "l") {
        var datas = new Array();
        for (var i = (new_game_data.length-1); i >= 0; i--) {
            datas.push(new_game_data[i][1]);
        }

        for (var i = 0; i < new_game_data.length; i++) {
            var vd = new_game_data[i];
            var t = vd[0].substr(10, 6);
            data[i] = {'date': t, 'visits': vd[1]}
        }

        data[data.length - 1].bulletClass = "lastBullet";
    } else {
        data = formatData(new_game_data);
    }
    amCharts.dataProvider = data;
    amCharts.validateData();
}

function InitializaM(vdata) {
    if (vdata.length == 0) return;

    var datas = new Array();
    for (var i = (vdata.length-1); i >= 0; i--) {
        datas.push(vdata[i][1]);
    }

    /*var max = Math.max.apply(null, datas);
    var min = Math.min.apply(null, datas);
    var interval = (max-min)/4.3;*/

    var data = [];
    for (var i = 0; i < vdata.length; i++) {
        var vd = vdata[i];
        var t = vd[0].substr(10, 6);
        data[i] = {'date': t, 'visits': vd[1]}
    }

    data[data.length - 1].bulletClass = "lastBullet";

    amCharts.dataProvider = data;
    amCharts.removeGraph(kline_amGraph);
    amCharts.removeGraph(line_amGraph);
    amCharts.addGraph(line_amGraph);
}

function InitializaK(vdata) {
    if (vdata.length == 0) return;
    var data = formatData(vdata);

    var datas = new Array();
    for (var i = (vdata.length-1); i >= 0; i--) {
        datas.push(vdata[i][4]);
    }

    /*var max = Math.max.apply(null, datas);
    var min = Math.min.apply(null, datas);
    var interval = (max-min)/4.3;*/

    amCharts.dataProvider = data;  //获取数据
    amCharts.removeGraph(line_amGraph);
    amCharts.removeGraph(kline_amGraph);
    amCharts.addGraph(kline_amGraph);
}

function addGuide(position_order, txt_len) {
    if (position_order != null) {
        var value1 = 0;
        var value2 = 0;
        var open_price = parseFloat(position_order.open_price);
        var order_contact = parseFloat(position_order.order_contact);
        if (position_order.type == 1) {
            value1 = parseFloat(open_price + order_contact).toFixed(txt_len);
            value2 = parseFloat(open_price - order_contact).toFixed(txt_len);
        } else {
            value1 = parseFloat(open_price - order_contact).toFixed(txt_len);
            value2 = parseFloat(open_price + order_contact).toFixed(txt_len);
        }

        guide1.value = value1;
        guide2.value = value2;
        amCharts.validateData();
    }
}

function delGuide() {
    guide1.value = -1;
    guide2.value = -1;
    amCharts.validateData();
}

//格式化数据
function formatData(vdata) {

    var data = [];
    for (var i = 0; i < vdata.length; i++) {
        var vd = vdata[i];
        data[i] = { 'date': getDataTime(vd[0]), 'open': vd[1], 'high': vd[2], 'low': vd[3], 'close': vd[4] }
    }
    return data;
}

//格式化时间
function getDataTime(v) {
    v=v.replace(/\//g, "-");
    var y = v.substring(5, 10);
    var t = v.substr(11, 5);
    return y + '\n' + t
}
